<template>
  <div class="tab">
    <div class="change-tab">
      <span
        v-for="(tab, index) in config"
        :key="tab.id"
        :class="{'tablinks': true, 'active': activeTab === index }"
        @click="changeTab(index)"
      >
        {{ tab.name }}
      </span>
    </div>
    
    <div class="record-detail">
      <div class="table-top">
        <div>{{ showText }} {{showNum}}</div>
        <div class="cause-select">
          <div>{{$t('member.components.title34')}}</div>
          <el-select v-model="tableObj.value" :placeholder="$t('common.base.pleaseSelect')" clearable @change="causeChange">
            <el-option
              v-for="item in config[activeTab].changeCause"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </div>
      </div>
      <el-table
          v-loading="listLoading"
          :data="tableObj.list"
          :highlight-current-row="false"
          border
          class="point-table"
          header-row-class-name="common-list-table-header"
          ref="multipleTable"
        >
          <template slot="empty">
            <BaseListForNoData
              v-show="!listLoading"
              :notice-msg="$t('common.base.noData')"
            ></BaseListForNoData>
          </template>
          <template v-for="(column, index) in config[activeTab].columns">
            <el-table-column
              :key="`${column.field}_${index}`"
              :label="column.label"
              :prop="column.field"
              :min-width="column.minWidth || '120px'"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <template v-if="column.field === 'createTime'">
                  {{ scope.row[column.field] | formatTime}}
                </template>
                <template v-else-if="column.field === 'changeCause'">
                  {{ scope.row[column.field] | causeFormat}}
                </template>
                <template v-else-if="column.field === 'num'">
                  <span v-if="activeTab == 0">-</span><span v-else>+</span>{{ scope.row[column.field]}}
                </template>
                <template v-else-if="column.field === 'validateTime'">
                  {{ scope.row.startTime | fmt_form_date}} {{$t('common.base.to')}} {{ scope.row.endTime | fmt_form_date}} 
                </template>
                <template v-else>
                  <div>
                    {{ scope.row[column.field] }}
                  </div>
                </template>
              </template>
            </el-table-column>
          </template>
        </el-table>
        <el-pagination
          class="product-table-pagination"
          background
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :page-sizes="[10, 20, 50]"
          :page-size="tableObj.pageSize"
          :current-page="tableObj.pageNum"
          layout="prev, pager, next, sizes, jumper"
          :total="tableObj.total"
        >
        </el-pagination>
    </div>
  </div>
</template>

<script>
import { 
  searchPointsConsume, 
  searchPointsRecharge, 
  searchBalanceConsume, 
  searchBalanceRecharge,
  searchServiceFeeRecharge,
  searchServiceFeeConsume,
  } from '@src/api/Member'
import RecordMixin from '@src/modules/member/mixins/record.js';
import dayjs from 'dayjs'
export default {
  name: 'record',
  mixins: [RecordMixin],
  props: {
    memberInfo: {
      type: Object,
      default: {}
    },
    config: {
      type: Array,
    },
    recordType: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      activeTab: 0,
      listLoading: false,
      tableObj: {
        list: [],
        value: '',
        pageSize: 10,
        pageNum: 1,
        total: 0
      }
    };
  },
  filters: {
    formatTime(time) {
      if (!time) return
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  computed: {
    showText() {
      if(this.recordType == 'points') return this.$t('member.components.title40')
      if(this.recordType == 'balance') return this.$t('member.fields.title13')
      return this.$t('member.components.title42')
    },
    showNum() {
      return this.memberInfo[this.recordType]
    },
    getDataFunction () {
      let getDataFunction = ''
      if(this.recordType == 'points') {
        getDataFunction = this.activeTab == 0 ? searchPointsConsume : searchPointsRecharge
      }else if(this.recordType == 'balance') {
        getDataFunction = this.activeTab == 0 ? searchBalanceConsume : searchBalanceRecharge
      }else {
        getDataFunction = this.activeTab == 0 ? searchServiceFeeConsume : searchServiceFeeRecharge
      }
      return getDataFunction
    }
  },
  methods: {
    changeTab(index) {
      this.activeTab = index
      this.tableObj.value = ''
      this.tableObj.pageNum = 1
      this.getTableData()
    },
    causeChange() {
      this.tableObj.pageNum = 1
      this.getTableData()
    },
    async getTableData() {
      let param = {
        membershipId: this.memberInfo.id,
        changeCause: this.tableObj.value,
        pageSize: this.tableObj.pageSize,
        pageNum: this.tableObj.pageNum,
      }
      this.listLoading = true
      const { code, message, result } = await this.getDataFunction(param);
      this.listLoading = false
      if(code !== 0) return this.$message.error(message);
      this.tableObj.list = result.list
      this.tableObj.total = result.total
    },
  }
};
</script>

<style lang="scss" scoped>
.change-tab {
  height: 48px;
  border-bottom: 1px solid #E8E8E8;
  display: flex;
  align-items: center;
  padding-left: 15px;
  >span:nth-child(1) {
    margin-right: 12px;
  }
  .tablinks {
    display: inline-block;
    width: 96px;
    height: 28px;
    line-height: 28px;
    text-align: center;
    border-radius: 14px;
    font-size: 12px;
    background: #F5F5F5;
    color: #262626;
  }
  .active {
    background: $color-primary-light-1;
    color: $color-primary-light-6;
  }
}
.record-detail{
  padding: 0 15px
}
.table-top {
  display: flex;
  height: 60px;
  align-items: center;
  justify-content: space-between;
  .cause-select {
    display: flex;
    align-items: center;
    >div {
      margin-right: 8px
    }
  }
}
</style>
